//
//  CallRecordViewController.m
//  NetPhone
//
//  Created by intel intel on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CallRecordViewController.h"
#import "NetPhoneDAO.h"
#import "CallRecord.h"
#import "CallRecrodCell.h"
#import "CallViewController.h"
#import "Common.h"
#import "CallRecordListViewController.h"
#import "iToast.h"
//
@implementation CallRecordViewController
@synthesize scrollviewDelegate;
@synthesize dataTableView;
@synthesize style;
@synthesize rows;

-(void)reloadCallRecords
{
    [rows removeAllObjects];
    NSArray *array = [[NetPhoneDAO sharedInstance]loadAllCallRecrod];
    [rows addObjectsFromArray:array];
    [dataTableView reloadData];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
        self.view.bounds=CGRectMake(0, -20, self.view.bounds.size.width, self.view.bounds.size.height+25);
    }
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    self.title = @"通话记录";
    //self.navigationController.delegate = self;
    
    rows = [[NSMutableArray alloc]init];
    //[self reloadCallRecords];
    
    dataTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-49-44) style:UITableViewStylePlain];
	dataTableView.dataSource = self;
	dataTableView.delegate = self;
    dataTableView.backgroundColor = [UIColor whiteColor];
    //dataTableView.separatorColor = [UIColor grayColor];
	[self.view addSubview:dataTableView];

    
    UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc]initWithTitle:@"清除" 
                                                                   style:UIBarButtonItemStylePlain 
                                                                  target:self action:@selector(clearRecord:)]autorelease];	
	self.navigationItem.rightBarButtonItem = rightButton;

}


/*- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self) {
        [self reloadCallRecords];
    }
}*/
-(void)viewWillAppear:(BOOL)animated
{
    [self reloadCallRecords];
    [super viewWillAppear:animated];
}

-(void)clearRecord:(id)sender
{
    UIActionSheet *menu = [[UIActionSheet alloc] 
                           initWithTitle: nil
                           delegate:self
                           cancelButtonTitle:@"取消"
                           destructiveButtonTitle:@"清除所有通话记录"
                           otherButtonTitles:nil];
    menu.tag = 1000;
    [menu showInView:self.view.superview];
    [menu release];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
{ 
	return 1; 
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
    return [rows count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 45;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row=[indexPath row]; 
    CallRecord *callRecord = [rows objectAtIndex:row];
    static NSString *defaultCellIdentifier = @"defaultCellIdentifier";
    CallRecrodCell *cell = (CallRecrodCell *)[tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
    if (cell == nil) {
        cell = [[[CallRecrodCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:defaultCellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        if (style ==1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        }
    }
    [cell setCallRecord:callRecord];
    return cell;
}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row=[indexPath row];
    CallRecord *callRecord = [rows objectAtIndex:row];
    
    CallRecordListViewController *crlvc = [[[CallRecordListViewController alloc]initWithCallRecord:callRecord]autorelease];
    [self.navigationController pushViewController:crlvc animated:YES];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	NSUInteger row=[indexPath row];
    CallRecord *callRecord = [rows objectAtIndex:row];
    self.personPhone=callRecord.phoneNum;
    self.userName = callRecord.phoneName;
    NSLog(@"===didSelectRowAtIndexPath===");
    NSString* money=[[NSUserDefaults standardUserDefaults] objectForKey:@"money"];
    if (money && [money isEqualToString:@"0"])
    {
        iToast* toast=[iToast makeText:@"余额不足,请充值后再使用!"];
        [toast show];
        return;
    }
    NSString* moneyDate=[[NSUserDefaults standardUserDefaults] objectForKey:@"moneyDate"];
    if ([moneyDate isEqualToString:@"1"])
    {
        iToast* toast=[iToast makeText:@"余额已过期,请充值后再使用!"];
        [toast show];
        return;
    }
    [self showCallTypeView];
    //[Common callWithNumber:callRecord.phoneNum name:callRecord.phoneName];
}
-(void)showCallTypeView
{
    NSString* callway = [[NSUserDefaults standardUserDefaults]objectForKey:@"DirectDial"];
    if (callway&& [callway isEqualToString:@"1"])
    {
        UIActionSheet* callMethdSheet1=[[UIActionSheet alloc]initWithTitle:@"温馨提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"直拨" otherButtonTitles:@"回拨", nil];
        [callMethdSheet1 showInView:self.view];
    }else{
        ////拨打类型 0.直拨  1.回拨
        [Common callWithNumber:self.personPhone name:self.userName AndTheType:1];
    }
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (actionSheet.tag == 1000)
    {
        if (buttonIndex == 0)
        {
            [[NetPhoneDAO sharedInstance]deleteAllCallRecord];
            [self reloadCallRecords];
        }
    }else
    {
        if (buttonIndex<=1)
        {
            if (self.userName && self.userName.length>0)
            {
                [Common callWithNumber:self.personPhone name:self.userName AndTheType:buttonIndex];
            }else
            {
                [Common callWithNumber:self.personPhone name:self.personPhone AndTheType:buttonIndex];
            }
        }
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollviewDelegate) {
        [scrollviewDelegate scrollViewWillBeginDragging:scrollView];
    }
}

-(void)dealloc
{
    [rows release];
    [super dealloc];
}

@end
